package com.fjwt.gz.proxy.ctrl.sysuser;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fjwt.gz.core.constants.CS;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.SysQaTypeDTO;
import com.fjwt.gz.db.entity.SysQaTypeEntity;
import com.fjwt.gz.db.vo.SysQaTypeVO;
import com.fjwt.gz.proxy.ctrl.CommonCtrl;
import com.fjwt.gz.service.impl.SysQaTypeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统QA类别表 前端控制器
 * </p>
 *
 * @author zhangyangyang
 * @since 2025-05-15
 */
@Slf4j
@RestController
@RequestMapping("/api/sysQaType")
public class SysQaTypeController extends CommonCtrl {

    @Autowired
    private SysQaTypeService sysQaTypeService;


    /**
     * 机构查询QA类别列表
     */
    @GetMapping
    public ApiRes list() {
        SysQaTypeDTO sysQaTypeDTO = getObject(SysQaTypeDTO.class);
        log.info("机构查询QA类别列表，请求地址：/api/sysQaType，请求参数：{}",  JSONUtil.toJsonStr(sysQaTypeDTO));
        LambdaQueryWrapper<SysQaTypeEntity> wrapper = SysQaTypeEntity.gw()
                .like(ObjectUtils.isNotEmpty(sysQaTypeDTO.getTypeName()),SysQaTypeEntity::getTypeName, sysQaTypeDTO.getTypeName())
                .eq(SysQaTypeEntity::getSysType, CS.SYS_ROLE_TYPE.PROXY)
                .orderByDesc(SysQaTypeEntity::getTypeSort);
        IPage<SysQaTypeVO> page = sysQaTypeService.page(getIPage(true),  wrapper);
        log.info("机构查询QA类别列表，请求地址：/api/sysQaType，返回结果：{}",  JSONUtil.toJsonStr(page));
        return ApiRes.page(page);
    }

    /**
     * 机构新增QA类别
     */
    @PreAuthorize("hasAnyAuthority('ENT_QA_TYPE_INFO_ADD')")
    @PostMapping("/add")
    public ApiRes add() {
        SysQaTypeDTO sysQaTypeDTO = getObject(SysQaTypeDTO.class);
        sysQaTypeDTO.setSysType(CS.SYS_ROLE_TYPE.PROXY);
        log.info("机构新增QA类别，请求地址：/api/sysQaType/add，请求参数：{}",  JSONUtil.toJsonStr(sysQaTypeDTO));
        sysQaTypeService.add(sysQaTypeDTO);
        return ApiRes.ok();
    }
    /**
     * 机构修改QA类别
     */
    @PreAuthorize("hasAnyAuthority('ENT_QA_TYPE_INFO_EDIT')")
    @PutMapping("/update")
    public ApiRes update() {
        SysQaTypeDTO sysQaTypeDTO = getObject(SysQaTypeDTO.class);
        sysQaTypeDTO.setSysType(CS.SYS_ROLE_TYPE.PROXY);
        log.info("机构修改QA类别，请求地址：/api/sysQaType/update，请求参数：{}",  JSONUtil.toJsonStr(sysQaTypeDTO));
        sysQaTypeService.update(sysQaTypeDTO);
        return ApiRes.ok();
    }
    /**
     * 机构删除QA类别
     */
    @PreAuthorize("hasAnyAuthority('ENT_QA_TYPE_INFO_DEL')")
    @DeleteMapping("/delete")
    public ApiRes delete() {
        SysQaTypeDTO sysQaTypeDTO = getObject(SysQaTypeDTO.class);
        sysQaTypeDTO.setSysType(CS.SYS_ROLE_TYPE.PROXY);
        log.info("机构删除QA类别，请求地址：/api/sysQaType/delete，请求参数：{}",  JSONUtil.toJsonStr(sysQaTypeDTO));
        sysQaTypeService.remove(sysQaTypeDTO);
        return ApiRes.ok();
    }
}

